﻿using System;

namespace DotNetStar.TheKing.Infrastructure
{
    public static class Extensions
    {
        public const string MOBILE_PATTERN = "^0?1[0-9]{10}$";
        ///// <summary>
        ///// 获取由验证实体的错误信息集合。
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns>错误信息集合。</returns>
        //public static IEnumerable<string> GetErrors(this IValidationModel model)
        //{
        //    var list = new List<ValidationResult>();
        //    var context = new ValidationContext(model);
        //    if(!Validator.TryValidateObject(model, context, list))
        //    {
        //        return list.Select(m => m.ErrorMessage);
        //    }

        //    return new List<string>();
        //}
        /// <summary>
        /// 获取正负值的数字字符串。如果大于0，则为 +x，否则就是当前数字。
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetPositiveNumString(this int num)
        => num > 0 ? $"+{num}" : num.ToString();

        /// <summary>
        /// 转换成人性化时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToHumanDateTimeString(this DateTime dateTime)
        {
            var time = DateTime.Now - dateTime;
            if (time.Days > 14)
            {
                return dateTime.ToString("yyyy-MM-dd HH:mm");
            }
            if (time.Days > 7 && time.Days < 14)
            {
                return "7天前";
            }
            if (time.Days > 3 && time.Days <= 7)
            {
                return "3天前";
            }

            if(time.Days > 0 && time.Days <= 3)
            {
                return "1天前";
            }
            if (time.Hours >0  && time.Hours <= 24)
            {
                return $"{time.Hours}小时前";
            }
            if(time.Minutes>0 && time.Minutes <= 60 )
            {
                return $"{time.Minutes}分钟前";
            }
            if (time.Seconds > 5 && time.Seconds <=60)
            {
                return $"{time.Seconds}秒前";
            }
            return "刚刚";
        }

        /// <summary>
        /// 转换成日期和周的字符串，12-23 周一
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToDateWeekString(this DateTime dateTime)
            => $"{dateTime.Date.ToShortDateString()} {dateTime.DayOfWeekChineseString("周")}";
    }
}
